﻿using System;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Test3();

            Console.WriteLine("program run over!");
        }

        /// <summary>
        /// 反射
        /// </summary>
        static void Test1()
        {
            #region
            Demo demo = new Demo("张三", 20);
            Console.WriteLine(demo.ToString());

            //反射获取程序集
            var type = nameof(Demo);
            Console.WriteLine(type);
            var assembly = Assembly.GetExecutingAssembly();
            Console.WriteLine(assembly.FullName);
            var types = assembly.GetTypes();
            foreach (var t in types)
            {
                Console.WriteLine(t.FullName);
            }

            //获取类型的几种形式
            var demoName = "ConsoleDemo.Demo";
            //var demoType = typeof(Demo);
            //var newDemoType = demo.GetType();
            //var demoType = assembly.GetType("ConsoleDemo.Demo");
            var demoType = Type.GetType(demoName);
            Console.WriteLine(demoType.FullName);

            var methodName = "Fun2";
            var methodInfo = demoType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
            Console.WriteLine(methodInfo.Name);
            var result = methodInfo.Invoke(demo, null);
            Console.WriteLine(result as string);

            //属性, 字段
            var propertyName = "age";
            var propertyInfo = typeof(Demo).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);
            Console.WriteLine(propertyInfo.Name);
            var value = propertyInfo.GetValue(demo);
            Console.WriteLine(value.ToString());
            var age = 18;
            propertyInfo.SetValue(demo, age);
            value = propertyInfo.GetValue(demo);
            Console.WriteLine(value.ToString());

            //反射构造方法的两种形式
            var ctor = typeof(Demo).GetConstructor(new Type[] { typeof(string), typeof(int) });
            var newDemo = ctor.Invoke(new object[] { "小三", 18 });
            var activatorDemo = Activator.CreateInstance(typeof(Demo), new object[] { "小三", 18 });
            var demoResult = activatorDemo.ToString();
            Console.WriteLine(demoResult);
            #endregion
        }
        /// <summary>
        /// 特性
        /// </summary>
        static void Test2()
        {
            var type = typeof(Demo);
            var att = type.GetProperty(nameof(Demo.Name)).GetCustomAttribute<ExcelAttribute>();
            Console.WriteLine(att.Column);
            var attAge = type.GetProperty(nameof(Demo.Age)).GetCustomAttribute<ExcelAttribute>();
            Console.WriteLine(attAge.Column);
        }
        /// <summary>
        /// 正则表达式
        /// </summary>
        static void Test3()
        {
            var phone = "12602900000";
            Regex regex = new Regex("1[3|5|7|8|9]\\d{9}", RegexOptions.Compiled);
            var result = regex.Match(phone);
            if (result.Success)
            {
                Console.WriteLine("phone your input is right!");
            }
            else
            {
                Console.WriteLine("phone your input is wrong!");
            }
        }
        /// <summary>
        /// dynamic
        /// </summary>
        static void Test4()
        {
            var type = typeof(Demo);
            Console.WriteLine(type.Name);

            var name = "小三";
            var age = 18;

            dynamic dy = name;
            Console.WriteLine(dy.ToString());

            dy = age;
            Console.WriteLine(dy.ToString());
        }
    }


    class Demo
    {
        public Demo()
        {
            Name = "李四";
            Age = 85;
        }
        public Demo(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public static void Fun1()
        {
            Console.WriteLine("this is fun1");
        }

        private void Fun2()
        {
            Console.WriteLine("this is fun2");
        }

        public override string ToString()
        {
            return $"{Name} now {Age} years old!";
        }

        [Excel("名字")]
        public string Name { get; set; }
        [Excel("年龄")]
        public int Age { get; set; }
    }

    public class ExcelAttribute : Attribute
    {
        public ExcelAttribute(string column)
        {
            Column = column;
        }

        public string Column { get; set; }
    }
}
